/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Aptech.DAL.MgUser;
import Aptech.DALInterface.UserInterface;
import Aptech.Common.BaseDispatchAction;
import Aptech.Common.Formater;
import Form.OrderForm;
import Form.UserForm;
import HibernateMappingPoJo.Orders;
import HibernateMappingPoJo.Users;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.util.UUID;
import org.apache.struts.actions.DispatchAction;

/**
 * 
 * @author nkhoan
 */
public class UserAction extends DispatchAction {

	UserInterface useInt = new MgUser();

	public ActionForward addUser(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String type = request.getParameter("type");
		request.setAttribute("sitemap", "Add A New User");
		if (type == null) {
			request.setAttribute("type", "add");
			return map.findForward("edit");
		} else {
			UserForm userForm = (UserForm) form;

			UUID id = UUID.randomUUID();
			Users user = new Users();
			user.setName(userForm.getName());
			user.setPass(userForm.getPass());
			user.setUserName(userForm.getUserName());
			if (userForm.getPosition() == Boolean.TRUE) {
				user.setPosition("Admin");
			} else {
				user.setPosition("User");
			}
			user.setId(id.toString());
			useInt.addUser(user);
			request.setAttribute("save", "save");
		}

		return map.findForward("edit");

	}

	public ActionForward getAllUser(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("sitemap", "List of User");
		UserForm userForm = (UserForm) form;
		List listUser = useInt.getAllUser();
		if (listUser != null) {
			Users[] list = new Users[listUser.size()];
			Iterator iter = listUser.iterator();
			int i = 0;
			while (iter.hasNext()) {
				Users tptd = (Users) iter.next();
				list[i] = tptd;
				i++;
			}
			userForm.setListUsers(list);
		}
		return map.findForward("list");
	}

	public ActionForward deleteUser(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		useInt.delete(id);
		response.sendRedirect("/NTB/user.do?method=getAllUser");

		return map.findForward("list");
	}

	public ActionForward editUser(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		request.setAttribute("sitemap", "Edit Of A User");
		UserForm userForm = (UserForm) form;
		if (type.equals("edit1")) {
			request.setAttribute("id", id);
			request.setAttribute("type", type);
			Users user = useInt.getUserByID(id);
			userForm.setId(user.getId());
			userForm.setName(user.getName());
			userForm.setPass(user.getPass());
			userForm.setUserName(user.getUserName());
			if (user.getPosition().equals("Admin")) {
				userForm.setPosition(Boolean.TRUE);
			} else {
				userForm.setPosition(Boolean.FALSE);
			}
			return map.findForward("edit");
		} else {

			Users user = new Users();
			user.setId(id);
			user.setName(userForm.getName());
			user.setPass(userForm.getPass());
			user.setUserName(userForm.getUserName());
			if (userForm.getPosition() == Boolean.TRUE) {
				user.setPosition("Admin");
			} else {
				user.setPosition("User");
			}
			useInt.editUser(user);
			request.setAttribute("save", "save");
		}

		return map.findForward("edit");
	}

	public ActionForward detailUser(ActionMapping map, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm userForm = (UserForm) form;
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        request.setAttribute("type", "detail");
        
        request.setAttribute("sitemap", "Information Detail of A User");
        // Set value for Formpe
        Users user = useInt.getUserByID(id);     
        userForm.setName(user.getName());
        userForm.setPass(user.getPass());
        userForm.setUserName(user.getUserName());     
        if (user.getPosition().equals("Admin")) {
			userForm.setPosition(Boolean.TRUE);
		} else {
			userForm.setPosition(Boolean.FALSE);
		}     
        userForm.setId(id);
		return map.findForward("edit");
	}
}
